﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Linq;
using System.Text;
using JKF.BusinessLayer.BLL;
using JKF.DataModule;
using JKF.DataModule.SqlServerDAL;
using JKF.Entities.Model;
using JKF.Substructure;


namespace JKF.BusinessLayer.BLL
{
    public partial class bllSMS_ACCOUNT
    {
        public DataTable GetSMSAccounts(string searchItem, string matchWay, string keyword, 
int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            dalSMS_ACCOUNT dalSmsAccount = new dalSMS_ACCOUNT();
            return dalSmsAccount.GetSMSAccounts(searchItem, matchWay, keyword, pageCurrent, pageSize, ref pageCount, ref rowCount);
        }

        public bool OperOrCloseAccount(string shopCode,string smsAccount)
        {
            var dbClass = DB.HR;

            try
            {
                var sqlText_PWD = " SELECT PWD  FROM   dbo.SMS_DIC_ACCOUNT WHERE ACCOUNT= @ACCOUNT";
                var cmd = dbClass.GetSqlStringCommand(sqlText_PWD);
                dbClass.AddInParameter(cmd, "ACCOUNT", DbType.String, smsAccount);
                object smsPwd = dbClass.ExecuteScalar(cmd);
                if (smsPwd == null)//系统未设置短信账号
                    return false;

                var get_account_sql = " SELECT SMS_ACCOUNT  FROM   dbo.SMS_ACCOUNT WHERE SHOP_CODE= @SHOP_CODE";
                var get_account_cmd = dbClass.GetSqlStringCommand(get_account_sql);
                dbClass.AddInParameter(get_account_cmd, "SHOP_CODE", DbType.String, get_account_sql);
                object account = dbClass.ExecuteScalar(get_account_cmd);

                if (account == null) //开通短信账号
                {
                    //添加短信充值日志
                    var sqltext_open = "INSERT INTO [SMS_ACCOUNT]([SMS_ACCOUNT],SMS_PWD,STATUS,SHOP_CODE,USER_CREATE)"
                        + "VALUES(@SMS_ACCOUNT,@SMS_PWD,'1',@SHOP_CODE,@USER_CREATE)";

                    var add_count_cmd = dbClass.GetSqlStringCommand(sqltext_open);
                    dbClass.AddInParameter(add_count_cmd, "SMS_ACCOUNT", DbType.String, smsAccount);
                    dbClass.AddInParameter(add_count_cmd, "SMS_PWD", DbType.String, smsPwd.ToString());
                    dbClass.AddInParameter(add_count_cmd, "SHOP_CODE", DbType.String, shopCode);
                    dbClass.AddInParameter(add_count_cmd, "USER_CREATE", DbType.Guid, bllSYS_USER.CurrentUser.USER_KEY);

                    dbClass.ExecuteNonQuery(add_count_cmd);
                }
                else //取消短信账号
                {
                    var sqltext_close = " UPDATE SMS_ACCOUNT SET STATUS='0' ";
                    dbClass.ExecuteNonQuery(sqltext_close);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
